package org.utils;

import java.util.Dictionary;
import java.util.Hashtable;

public class AnalogConvertor {
	
	private static final String[] firstRank = {"нөл", "бір", "екі", "үш", "төрт", "бес", "алты", "жеті", "сегіз", "тоғыз"};
	private static final String[] secondRank = {"", "он", "жиырма", "отыз", "қырық", "елу", "алпыс", "жетпіс", "сексен", "тоқсан"};
	private static final String[] firdtRank = {"жүз"};
	private static final String[] fourth = {"мын"};
	private static final String[] fiveth = {"миллион"};
	private static final String[] sixth = {"миллиард"};
	
	private static Dictionary<String, String> numbers = new Hashtable<String, String>();
	
	static
	{
		numbers.put("0", "нөл");
		numbers.put("1", "бір");
		numbers.put("2", "екі");
		numbers.put("3", "үш");
		numbers.put("4", "төрт");
		numbers.put("5", "бес");
		numbers.put("6", "алты");
		numbers.put("7","жеті");
		numbers.put("8", "сегіз");
		numbers.put("9", "тоғыз");
		numbers.put("1r", "он");
		numbers.put("2r", "жиырма");
		numbers.put("3r", "отыз");
		numbers.put("4r", "қырық");
		numbers.put("5r", "елу");
		numbers.put("6r", "алпыс");
		numbers.put("7r", "жетпіс");
		numbers.put("8r", "сексен");
		numbers.put("9r", "тоқсан");
		numbers.put("rr", "жүз");
		numbers.put("kk", "мын");
		numbers.put("kkk", "миллион");
		numbers.put("kkkk", "миллиард");
		numbers.put("k", "");
	}
	
	public static String convertToString(String number)
	{
		int index = number.indexOf(".") == -1 ? number.length() : number.indexOf(".");
		
		int num1 = Integer.valueOf(number.substring(0, index));
		int num2 = 0;
		if(index != number.length())
			num2 = Integer.valueOf(number.substring(index+1, number.length()));		
		
		return getStrNumber(num1).trim() + " тенге " + getStrNumber(num2).trim() + " тиын";
	}
	
	private static String getStrNumber(int number)
	{
		String str = "";
		int temp = 0;
		String key = "k";
		
		if(number == 0)
			return numbers.get(String.valueOf(number));
		
		while(number > 0)
		{
			temp = number % 1000;
			if(temp != 0)
				str = thirdRank(String.valueOf(temp)) + " " + numbers.get(key) + " " + str;
			number = (int)(number / 1000);
			key += "k";
		}
		return str;
	}
	
	private static String thirdRank(String number)
	{
		String strNumber = "";
		
		String key = "";
		
		int index  = 0;
		
		int buf = 0;
		
		for(int i = number.length(); i > 0; i--)
		{
			buf = Integer.valueOf(number.substring(i-1,i));
			if(buf > 0)
			{	
				if(index > 1)
					strNumber = numbers.get(number.substring(i-1,i)) + " " + numbers.get(key) + " " + strNumber;
				else
					strNumber = numbers.get(number.substring(i-1,i)+key) + " " + strNumber;
			}
			key += "r";
			index++;
		}
		
		return strNumber;
	}
}
